#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE 256
#define NUM_BUFFERS 64
#define MAX_FILES 16

int main(int argc, char* argv[]) {
    if (argc < 4) {
        fprintf(stderr, "Usage: HexMer files [file...] -o output.hex\n");
        return 1;
    }

    char* output_file = NULL;
    FILE* input_files[MAX_FILES];
    int input_count = 0;
    
    // 解析命令行参数
    for (int i = 1; i < argc; i++) {
        if (strcmp(argv[i], "-o") == 0) {
            if (i + 1 < argc) {
                output_file = argv[++i];
            } else {
                fprintf(stderr, "Error: No output file specified after -o\n");
                return 1;
            }
        } else {
            if (input_count < MAX_FILES) {
                FILE* fp = fopen(argv[i], "r");
                if (!fp) {
                    fprintf(stderr, "Failed to open %s\n", argv[i]);
                    // 关闭已打开的文件
                    while (input_count--) fclose(input_files[input_count]);
                    return 1;
                }
                input_files[input_count++] = fp;
            } else {
                fprintf(stderr, "Too many input files (max %d)\n", MAX_FILES);
                return 1;
            }
        }
    }

    if (!output_file) {
        fprintf(stderr, "Error: Output file not specified\n");
        return 1;
    }

    // 打开输出文件
    FILE* fp_out = fopen(output_file, "w");
    if (!fp_out) {
        perror("Failed to open output file");
        // 关闭输入文件
        for (int i = 0; i < input_count; i++) fclose(input_files[i]);
        return 1;
    }

    // 处理每个输入文件
    for (int i = 0; i < input_count; i++) {
        char buffers[NUM_BUFFERS][MAX_LINE];
        int line_lengths[NUM_BUFFERS];
        int buf_index = 0;
        
        // 读取并缓存数据
        while (fgets(buffers[buf_index], MAX_LINE, input_files[i])) {
            // 移除换行符
            char* nl = strchr(buffers[buf_index], '\n');
            if (nl) *nl = '\0';
            
            line_lengths[buf_index] = strlen(buffers[buf_index]);
            buf_index++;
            
            // 缓存满时处理
            if (buf_index == NUM_BUFFERS) {
                int end_idx = (i == input_count - 1) ? NUM_BUFFERS : 
                             (buf_index - (line_lengths[buf_index-1] == 11 && 
                              strncmp(buffers[buf_index-1], ":00000001FF", 11) == 0));
                
                // 写入数据
                for (int j = 0; j < end_idx; j++) {
                    fprintf(fp_out, "%s\n", buffers[j]);
                }
                
                // 清除已处理的缓冲区
                if (i != input_count - 1) {
                    // 移除非最后一个文件的EOF标记
                    for (int j = end_idx; j < NUM_BUFFERS; j++) {
                        if (line_lengths[j] == 11 && 
                            strncmp(buffers[j], ":00000001FF", 11) == 0) {
                            break;
                        }
                        fprintf(fp_out, "%s\n", buffers[j]);
                    }
                }
                
                buf_index = 0;
            }
        }

        // 处理剩余数据
        if (buf_index > 0) {
            // 最后一个文件保留所有行
            if (i == input_count - 1) {
                for (int j = 0; j < buf_index; j++) {
                    fprintf(fp_out, "%s\n", buffers[j]);
                }
            } 
            // 非最后一个文件移除EOF
            else {
                int end_idx = buf_index;
                if (line_lengths[buf_index-1] == 11 && 
                    strncmp(buffers[buf_index-1], ":00000001FF", 11) == 0) {
                    end_idx--;
                }
                
                for (int j = 0; j < end_idx; j++) {
                    fprintf(fp_out, "%s\n", buffers[j]);
                }
            }
        }
        
        fclose(input_files[i]);
    }
    
    fclose(fp_out);
    return 0;
}